﻿/*--------------------------------------------------------------
 * <copyright file="WorkEfficiencyQuery.cs" company="O-RID">
 * Copyright (c)2010 O-RID Co.,Ltd.
 * </copyright>
 * <author>Li Dawei</author>
 * <summary> 
 *          稼动率 查询
 *          作成日：08/20/2010
 *  </summary>
 *------------------------------------------------------------
*/
namespace Orid.Management.Statistics.OperatorUnitMangement.Command.Matter
{
    using System;
    using System.Windows;
    using System.Windows.Media;
    using Orid.Management.Statistics.OperatorUnitMangement.Common.FunctionList;
    using Orid.Management.Statistics.OperatorUnitMangement.Common.SearchCtrl;
    using Orid.Management.Statistics.OperatorUnitMangement.WCFServers.MatterQueryRef;
    using childWindow = Orid.Management.Statistics.OperatorUnitMangement.Common.Window;
    using Wcf = Orid.Management.Statistics.OperatorUnitMangement.Common.Service;

    /// <summary>
    ///  稼动率 查询
    /// </summary>
    public class WorkEfficiencyQuery : Orid.Management.Statistics.OperatorUnitMangement.Command.ICommand
    {
        #region  全局变量定义
        /// <summary>
        /// 查询区域对象 workEfficSelcontainer
        /// </summary>
        private USEfficSelContainer workEfficSelcontainer;

        /// <summary>
        /// 错误消息提示 ErrorMessagebox
        /// </summary>
        private childWindow.MessageBox errorMessbox;

        /// <summary>
        /// 日志服务定义 wcfLogClient
        /// </summary>
        private WCFLog4Ref.WCFLog4Client wcfLogClient;

        /// <summary>
        /// 查询服务定义 WCFMatterCorrelationClient
        /// </summary>
        private WCFServers.MatterQueryRef.WCFMatterCorrelationClient wcfWorkEfficiencyQueryClient;

        #endregion  全局变量定义

        /// <summary>
        /// ジョブ化率 查询
        /// </summary>
        /// <param name="USEfficSelContainer">The matter selcontainer.</param>
        public WorkEfficiencyQuery(USEfficSelContainer workEfficSelcontainer)
        {
            this.workEfficSelcontainer = workEfficSelcontainer;
            //隐藏稼动率类型选择
            this.workEfficSelcontainer.cbWorkEfficiency.Visibility = Visibility.Collapsed;
            this.workEfficSelcontainer.lbEffciency.Visibility = Visibility.Collapsed;

            this.InitField();
        }

        /// <summary>
        /// 初始化变量 InitField
        /// </summary>  
        private void InitField()
        {
            this.wcfWorkEfficiencyQueryClient = Wcf.Service.WorkEfficiencyQuery;

            this.errorMessbox = new childWindow.MessageBox();

            this.wcfLogClient = Wcf.Service.Log4;

            this.wcfWorkEfficiencyQueryClient.SelectReportWorkEfficiencyQueryInforCompleted +=
                                new EventHandler<SelectReportWorkEfficiencyQueryInforCompletedEventArgs>(this.OnExecover);
        }

        /// <summary>
        /// 查询完成事件 ExecOver
        /// </summary>
        public event EventHandler<SelectReportWorkEfficiencyQueryInforCompletedEventArgs> ExecOver;

        /// <summary>
        /// 查询开始事件 ExecBegin
        /// </summary>
        public event EventHandler<EventArgs> ExecBegin;

        /// <summary>
        /// 查询结束事件 OnExecOver
        /// </summary>
        /// <param name="sender">Sender of Object</param>
        /// <param name="e">e type of SelectReportWorkEfficiencyQueryInforCompletedEventArgs</param>
        protected void OnExecover(object sender, SelectReportWorkEfficiencyQueryInforCompletedEventArgs e)
        {
            if (this.ExecOver != null)
            {
                this.ExecOver(this, e);
            }
        }

        /// <summary>
        /// 查询开始事件 OnExecBegin
        /// </summary>
        /// <param name="sender">Sender of Object</param>
        /// <param name="e">e type of EventArgs</param>
        protected void OnExecBegin(object sender, EventArgs e)
        {
            if (this.ExecBegin != null)
            {
                this.ExecBegin(this, e);
            }
        }

        #region ICommand 成员

        /// <summary>
        /// 稼动率 查询 实现接口ICommand
        /// </summary>
        public void Exec()
        {
            try
            {
                int error = FunctionClass.IsSelectDo(
                                                   this.workEfficSelcontainer.dpDateFrom,
                                                   this.workEfficSelcontainer.dpDateTo,
                                                   this.errorMessbox
                                                   );
                if (error == 0)
                {
                    //查询按钮
                    this.workEfficSelcontainer.btnUnitSelect.IsEnabled = true;
                    return;
                }
                // 传送到服务端的参数列表
                WCFServers.MatterQueryRef.WhereParameters fitWhereData = new WhereParameters();

                try
                {
                    // 日期的判断条件
                    // 日期返回空值用"null"
                    if (!String.IsNullOrEmpty(this.workEfficSelcontainer.dpDateFrom.Text))
                    {

                        fitWhereData.WhereDateFrom = this.workEfficSelcontainer.dpDateFrom.SelectedDate.Value.ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        fitWhereData.WhereDateFrom = "null";
                    }

                    if (!String.IsNullOrEmpty(this.workEfficSelcontainer.dpDateTo.Text))
                    {

                        fitWhereData.WhereDateTo = this.workEfficSelcontainer.dpDateTo.SelectedDate.Value.ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        fitWhereData.WhereDateTo = "null";
                    }
                    
                    //两个日期相差的天数
                    fitWhereData.DateDiffs = FunctionClass.DateDiff(
                                        Convert.ToDateTime(fitWhereData.WhereDateFrom),
                                        Convert.ToDateTime(fitWhereData.WhereDateTo)
                                        );

                    //记录开始日期
                    this.workEfficSelcontainer.StartDataTime = Convert.ToDateTime(fitWhereData.WhereDateFrom);
                }
                catch
                {
                    throw new Exception("where格式转换出错！");
                }

                this.OnExecBegin(null, null);

                //调用  稼动率 查询
                this.wcfWorkEfficiencyQueryClient.SelectReportWorkEfficiencyQueryInforAsync(fitWhereData);

                //查询按钮不可用
                this.workEfficSelcontainer.btnUnitSelect.IsEnabled = false;

            }
            catch (Exception ex)
            {
                this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr + " 稼动率查询 接口实现类 WorkEfficiencyQueryClass" + ex.Message.ToString());

                //查询按钮可用
                this.workEfficSelcontainer.btnUnitSelect.IsEnabled = true;

                FunctionClass.ShowMessageBox(
                           this.errorMessbox,
                           1,
                           "提示",
                           ex.Message.ToString(),
                           Colors.Gray,
                           true,
                           Visibility.Visible,
                           Visibility.Collapsed
                           );
                return;
            }
        }
        #endregion
    }
}
